<template>
  <div class="auth-container">
    <div class="auth-card">
      <h2 class="auth-title">找回密码</h2>

      <form @submit.prevent="submitResetRequest" class="auth-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
              type="text"
            id="username"
            v-model="form.username"
            placeholder="请输入您的用户名"
            required
            class="form-input"
          >
        </div>

        <div class="form-group">
          <label for="email">注册邮箱</label>
          <input
            type="email"
            id="email"
            v-model="form.email"
            placeholder="请输入注册时使用的邮箱"
            required
            class="form-input"
          >
        </div>

        <div class="form-group">
          <label for="role">用户类型</label>
          <select
            id="role"
            v-model="form.role"
            class="form-select"
            required
          >
            <option value="normal">普通用户</option>
            <option value="labAdmin">实验室管理员</option>
            <option value="sysAdmin">系统管理员</option>
          </select>
        </div>

        <button type="submit" class="submit-btn" :disabled="isSubmitting">
          {{ isSubmitting ? '发送中...' : '发送重置请求' }}
        </button>
      </form>

      <div class="auth-footer">
        <router-link to="/" class="auth-link">返回登录</router-link>
        <span class="divider">|</span>
        <router-link to="/register" class="auth-link">注册账号</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ForgotPassword',
  data () {
    return {
      form: {
        username: '',
        email: '',
        role: '1'
      },
      isSubmitting: false
    }
  },
  methods: {
    async submitResetRequest () {
      this.isSubmitting = true
      try {
        const { data } = await this.$axios.post('/api/password-reset/request-reset/', {
          username: this.form.username,
          email: this.form.email,
          role: this.form.role,
          headers: {
            'Content-Type': 'application/json'
          }
        })
        this.$notify({
          title: '成功',
          message: data.detail,
          type: 'success'
        })
      } catch (error) {
        this.$notify.error({
          title: '错误',
          message: error.response?.data?.detail || '请求失败'
        })
      } finally {
        this.isSubmitting = false
      }
    }
  }
}
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  padding: 30px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.auth-title {
  text-align: center;
  color: #333;
  margin-bottom: 24px;
  font-size: 22px;
  font-weight: 500;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #555;
  font-size: 14px;
}

.form-input, .form-select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
  box-sizing: border-box;
}

.form-input:focus, .form-select:focus {
  border-color: #4CAF50;
  outline: none;
}

.form-select {
  appearance: none;
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 16px;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-top: 10px;
}

.submit-btn:hover:not(:disabled) {
  background-color: #45a049;
}

.submit-btn:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.auth-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  font-size: 14px;
}

.auth-link {
  color: #4CAF50;
  text-decoration: none;
  transition: color 0.2s;
}

.auth-link:hover {
  color: #3d8b40;
}

.divider {
  margin: 0 10px;
  color: #999;
}
</style>
